﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using LGSVR.Common;
using LGSVR.BLL.Program;
using System.Web.UI.HtmlControls;

namespace LGSVR_WEB.sys
{
    public partial class UserActMenuMain : LGSVR.Common.DataBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.parent.navigate('../Logout.aspx');</script>");
            }
            if (!IsPostBack)
            {
                //MsgContent = ShowMessage("请先在左边选择角色，再进行权限配置。", MessageType.Error, false, true);


                if (!string.IsNullOrEmpty(Request["actID"]))
                {
                    lblmessageCard.Text = "<span>当前选择角色为" + Request["ActName"] + "，请进行权限配置。</span>";
                    gvDataBind("VActMenu"," AND actID = '" + Request["actID"] +"'");
                    //超级管理员，不让编辑。这里超级管理员ID不确定，故用名称
                    if (Request["ActName"].Contains("超级"))
                    {
                        lbtnActSet.Visible = false;
                    }
                }
                else
                {
                    lblmessageCard.Text = "<span style=\"color: #FF0000\">请先在左边选择角色，再进行权限配置。</span>";
                    //gvDataBind(" AND 1=1 group by ");
                    gvDataBind("VVActMenu", "");

                }

            }
         
        }

      

        private void gvDataBind(string strDBName,string p)
        {
            DataTable dt;
            int totalRS;
            //int pageIndex;


            //if (!string.IsNullOrEmpty(Request.QueryString["pg"]) && Utils.IsNumber(Request.QueryString["pg"]))
            //{
            //    pageIndex = int.Parse(Request.QueryString["pg"]);
            //}
            //else
            //{
            //    pageIndex = 1;
            //}

            dt = GetList(strDBName, " id desc ", p, 1, Int32.MaxValue, out totalRS);
            rptBody.DataSource = dt;
            rptBody.DataBind();
            SplitHtmlCode = Split.GetHtmlCode("UserActMenuMain.aspx", 8, 1, Int32.MaxValue, totalRS);

        }

        protected void lbtnActSet_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request["actID"]))
            {
                MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
            }
            ActMenu actmenu = new ActMenu();
            bool isTrue = actmenu.UpdateState("VActMenu", false, "ActID", Request["actID"], Session["UserID"].ToString());
            if (isTrue)
            {
                string strIDs = string.Empty;
                for (int i = 0; i <= rptBody.Items.Count - 1; i++)
                {
                    HtmlInputCheckBox cbox = (HtmlInputCheckBox)rptBody.Items[i].FindControl("chkId");

                    if (cbox.Checked == true)
                    {
                        strIDs += cbox.Value + ",";
                    }
                }
                strIDs = strIDs.TrimEnd(',');
                isTrue = actmenu.UpdateState("VActMenu", true, "ID", strIDs, Session["UserID"].ToString());
                if (isTrue)
                {
                    MsgContent = ShowMessage("恭喜您，权限赋予成功！", MessageType.Success, false, true);
                    NoRefresh();
                    return;
                }
            }
                //else
                //{
                    MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
            //    }
            //}
        }

        protected void rptBody_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 

            {
                HtmlInputCheckBox cb = (HtmlInputCheckBox)e.Item.FindControl("chkId");
                //string sss = ((DataRowView)e.Item.DataItem)["menuID"].ToString();
                //string s11ss = ((DataRowView)e.Item.DataItem)["state"].ToString();
                //bool bl = Convert.ToBoolean(s11ss);

                cb.Checked = Convert.ToBoolean(((DataRowView)e.Item.DataItem)["state"]);



            }  

        }


       

      
    }
}